Skip to content

fix(parser): Kafka Envelope + tests #3489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 20, 2025
Merged

fix(parser): Kafka Envelope + tests #3489

merged 7 commits into from
Jan 20, 2025

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Jan 17, 2025

Summary

Changes

Please provide a summary of what's being changed

This PR updates the Kafka Envelope schema and logic by making sure that the result is a list of values rather than a list that contains a list of values, i.e. the result went from this [['{"key":"value"}']] to this ['{"key":"value"}'].

The linked issue contains an explanation of why the result was the way it was and also why that is a bug, but essentially a Lambda function can subscribe to a single topic/partition and thus there's no need to accommodate multiple lists of values since all values will inevitably belong to the same topic/partition combination.

While working on the PR, I also removed from the envelope the forced JSON transform of the values which was causing the envelope to not work with values that were anything other than a JSON-stringified string.

For example, the following payload, which has aGVsbG8= (aka "hello world") as value would not have worked because the envelope was always trying to run JSON.parse() on the decoded value:

{
  "eventSource": "aws:kafka",
  "eventSourceArn": "arn:aws:kafka:us-east-1:0123456789019:cluster/SalesCluster/abcd1234-abcd-cafe-abab-9876543210ab-4",
  "bootstrapServers": "b-2.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092,b-1.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092",
  "records": {
    "mytopic-0": [
      {
        "topic": "mytopic",
        "partition": 0,
        "offset": 15,
        "timestamp": 1545084650987,
        "timestampType": "CREATE_TIME",
        "key": "cmVjb3JkS2V5",
        "value": "eyJrZXkiOiJ2YWx1ZSJ9",
        "headers": [
          {
            "headerKey": [104, 101, 97, 100, 101, 114, 86, 97, 108, 117, 101]
          }
        ]
      }
    ]
  }
}

The new logic will stop at the base64 decoding and not make assumptions on the actual contents of the string. Customers can still use the JSONStringified we provide to do the parse and transform if they know it's of an appropriate type. For example, they can parse this event where value is "eyJrZXkiOiJ2YWx1ZSJ9" (aka "{\"key\":\"value\"}"):

{
  "eventSource": "aws:kafka",
  "eventSourceArn": "arn:aws:kafka:us-east-1:0123456789019:cluster/SalesCluster/abcd1234-abcd-cafe-abab-9876543210ab-4",
  "bootstrapServers": "b-2.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092,b-1.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092",
  "records": {
    "mytopic-0": [
      {
        "topic": "mytopic",
        "partition": 0,
        "offset": 15,
        "timestamp": 1545084650987,
        "timestampType": "CREATE_TIME",
        "key": "cmVjb3JkS2V5",
        "value": "eyJrZXkiOiJ2YWx1ZSJ9",
        "headers": [
          {
            "headerKey": [104, 101, 97, 100, 101, 114, 86, 97, 108, 117, 101]
          }
        ]
      }
    ]
  }
}

like this: KafkaEnvelope.parse(event, JSONStringified(z.object({ key: z.string() }))) (which will work also when using the Middy.js middleware or decorator.

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: fixes #3488


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@dreamorosi dreamorosi self-assigned this Jan 17, 2025
@dreamorosi dreamorosi requested a review from a team January 17, 2025 14:17
@dreamorosi dreamorosi requested a review from a team as a code owner January 17, 2025 14:17
@boring-cyborg boring-cyborg bot added parser This item relates to the Parser Utility tests PRs that add or change tests labels Jan 17, 2025
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Jan 17, 2025
@dreamorosi dreamorosi requested a review from am29d January 17, 2025 14:17
@github-actions github-actions bot added the bug Something isn't working label Jan 17, 2025
Copy link
Contributor

@am29d am29d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor comment on the bootstrapServers check.

@dreamorosi dreamorosi requested a review from am29d January 20, 2025 10:45
Copy link
Contributor

@am29d am29d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@am29d am29d merged commit bd6b24a into main Jan 20, 2025
38 checks passed
@am29d am29d deleted the fix/kafka_parser branch January 20, 2025 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser This item relates to the Parser Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Kafka envelope return an array of arrays of values
2 participants